import datetime
import json
import re

from re_common.baselibrary.tools.all_requests.aiohttp_request import AioHttpRequest
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.utils.basedict import BaseDicts
from re_common.baselibrary.utils.basetime import BaseTime

from apps.crawler_platform.core_platform.core_g import SQLTable, CoreSqlValue
from apps.crawler_platform.core_platform.g_model import DealModel, CallBackModel, \
    DealInsertModel, PaperDaysModel, DealUpdateModel, PaperPageModel, PaperArticleModel
from settings import get_settings

__all__ = [
            "zhongguokexuebao_zhongguokexuebaodays_callback",
            "zhongguokexuebao_zhongguokexuebaoarticle_callback",
           ]


def zhongguokexuebao_zhongguokexuebaodays_callback(callmodel: CallBackModel[PaperDaysModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "task_tag_next": task_info.task_tag_next}
    if "1_1" in data:
        di_model = DealInsertModel()
        di_model.insert_pre = CoreSqlValue.insert_ig_it
        for col_item, art_item in zip(data["1_1"]['col_info']['children'], data["1_1"]['art_info']['children']):
            for item in art_item['children']:
                col_title = col_item['col_title']
                col_url_part = ''
                year = callmodel.sql_model.year
                months = callmodel.sql_model.months
                days = callmodel.sql_model.days
                page = re.findall('第(.*?)版', col_title)[0]
                title = item['title']
                url_part = item['url_part']
                rawid_page = year + months + days + page.rjust(2, '0')
                index = re.findall(f'id=(.*)', url_part)[0]

                temp_info = info_dicts.copy()
                task_tag_next = temp_info["task_tag_next"]
                temp_info["task_tag"] = task_tag_next
                del temp_info["task_tag_next"]
                temp_info["rawid"] = rawid_page + index
                new_dict = dict()
                new_dict["cid"] = rawid_page + index
                new_dict["year"] = year
                new_dict["months"] = months
                new_dict["days"] = days
                new_dict["versions_title"] = f'({page.rjust(2, "0")})：{col_title.split(" ")[-1]}'
                new_dict["title"] = title
                new_dict["page"] = page.rjust(2, "0")
                new_dict["index"] = index
                new_dict["url"] = re.findall("sbhtmlnews.*", url_part)[0]
                new_dict["versions_url"] = ''

                temp_info["article_json"] = json.dumps(new_dict, ensure_ascii=False)
                di_model.lists.append(temp_info)

        result.next_dicts.insert.append(di_model)

    return result


def zhongguokexuebao_zhongguokexuebaoarticle_callback(callmodel: CallBackModel[PaperArticleModel]) -> DealModel:
    result = DealModel()
    return result